'use strict';

const await = require('await-stream-ready/lib/await');

const Controller = require('egg').Controller;

class UserController extends Controller {
    // 注册
    async reg() {
        let { ctx,app } = this
        ctx.validate({
            username:{
                type: 'string',
                required: true,
                range: {
                    min: 5,
                    max: 20
                },
                desc: '用户名'
            },
            password:{
                type: 'string',
                required: true,
                desc: '密码'
            },
            repassword:{
                type: 'string',
                required: true,
                desc: '确认密码'
            }
        })

        let { username,password,repassword } = ctx.request.body

        if(password !== repassword){
            ctx.throw(422,'密码和确认密码不一致')
        }

        // 验证用户是否已经存在
        if((await app.model.User.findOne({
            where:{
                username
            }
        }))){
            ctx.throw(400,'该用户名已存在')
        }

        let user = await app.model.User.create({
            username,
            password
        })

        if(!user){
            ctx.throw(400,'创建用户失败')
        }

        ctx.apiSuccess(user)
    }

    // 登录
    async login() {
        const { ctx, app } = this;
        // 参数验证
        ctx.validate({
            username:{
                type: 'string',
                required: true,
                desc: '用户名'
            },
            password:{
                type: 'string',
                required: true,
                desc: '密码'
            },
        })

        let { username, password } = ctx.request.body;

        let user = await app.model.User.findOne({
            where:{
                username
            }
        })

        if(!user){
            ctx.throw(400,'该用户不存在')
        }

        // 验证密码
        await ctx.checkPassword(password,user.password)

        user = JSON.parse(JSON.stringify(user))

        // 生成token
        user.token = ctx.getToken(user)
        delete user.password

        // 加入到存储中
        if(!(await this.service.cache.set('user_' + user.id,user.token))){
            ctx.throw(400,'登录失败')
        }

        ctx.apiSuccess(user)
    }

    // 退出登录
    async logout() {
        const { ctx,service } = this
        let current_user_id = ctx.authUser.id

        if(!(await service.cache.remove('user_'+current_user_id))){
            ctx.throw(400,'退出登录失败')
        }

        ctx.apiSuccess('ok')
    }

    // 获取当前用户信息
    async info(){
        const { ctx } = this
        let user = JSON.parse(JSON.stringify(ctx.authUser))
        delete user.password
        ctx.apiSuccess(user)
    }

    async update(){
        const { ctx, app } = this;
        let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
        let data =await app.model.User.findByPk(id);
        if(!data){
            ctx.throw(400,'该用户不存在')
        }
        ctx.validate({
            username:{
                type: 'string',
                required: true,
                range: {
                    min: 5,
                    max: 20
                },
                desc: '用户名'
            },sex:{
                type: 'int',
                required: true,
                range: {
                    min: 0,
                    max: 3
                },
                desc: '性别'
            },location:{
                type: 'string',
                required: false,
                desc: '位置'
            },petRelation:{
                type: 'int',
                required: true,
                range: {
                    min: 0,
                    max: 3
                },
                desc: '宠物关系'
            },birthday:{
                type: 'date',
                required: false,
                desc: '生日'
            },avatar:{
                type: 'string',
                required: false,
                defaultValue: '',
                desc: '头像'
            }
        })
        let { username,sex,location,petRelation,birthday,avatar } = ctx.request.body
        data.username = username;
        data.sex = sex;
        data.location =location;
        data.petRelation=petRelation
        data.birthday=birthday
        data.avatar=avatar;
        let res = await data.save();
        ctx.apiSuccess(res)
    }

    // 绑定邮箱
    async bindEmail(){
        const { ctx, app } = this;
        let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
        let data =await app.model.User.findByPk(id);
        if(!data){
            ctx.throw(400,'该用户不存在')
        }
        ctx.validate({
            email:{
                type: 'string',
                required: true,
                range: {
                    min: 5,
                    max: 30
                },
                desc: '邮箱'
            }
    })
    let { email } = ctx.request.body
    data.email=email
    let res = await data.save();
    ctx.apiSuccess(res)
    }

    // 举报
    async feedback(){
        const { ctx, app } = this;
        ctx.validate({
            informerId:{
                type: 'int',
                required: true,
                range: {
                    min: 1,
                    max: 20
                },
                desc: '举报人ID'
            },
            reportedId:{
                type: 'int',
                required: true,
                range: {
                    min: 1,
                    max: 20
                },
                desc: '被举报人ID'
            },
            content:{
                type: 'string',
                required: true,
                range: {
                    min: 1,
                    max: 600
                },
                desc: '举报内容'
            }
    })
    let { informerId,reportedId,content } = ctx.request.body
    let feedback = await app.model.Feedback.create({
        informerId,
        reportedId,
        content
    })
    ctx.apiSuccess(feedback)
    }

    //修改密码
    async updatepassword(){
        const {ctx, app} = this;
        let id = ctx.params.id ? parseInt(ctx.params.id) : 0;
        let data =await app.model.User.findByPk(id);
        if(!data){
            ctx.throw(400,'该用户不存在')
        }
        ctx.validate({
            password:{
                type: 'string',
                required: true,
                range: {
                    min: 6
                },
                desc: '密码'
            }
        })

        let {password} = ctx.request.body

        data.password = password;
        let res = await data.save();

        ctx.apiSuccess(res)
    }

    //获取指定用户信息
    async getcounts(){
        const { ctx,app } = this
        let user_id = ctx.params.user_id;
        let result;
        result = await app.model.User.findOne({
            where: {
                id:user_id
            }
        })
        delete result.password
        if(!result){
            ctx.throw(400,'用户不存在')
        }
        ctx.apiSuccess(result)
    }

    //获取目标用户信息
    async getinfo(){
        const { ctx,app } = this
        let id = ctx.params.id;
        let result;
        result = await app.model.User.findOne({
            where: {
                id:id
            }
        })
        delete result.password
        if(!result){
            ctx.throw(400,'用户不存在')
        }
        ctx.apiSuccess(result)
    }

}

module.exports = UserController;
